﻿using DeviceManager.ContentPage.SysMng;
using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceManager.ContentPage
{
    /// <summary>
    /// StatisticsPage.xaml 的交互逻辑
    /// </summary>
    public partial class StatisticsPage : Page
    {
        public StatisticsPage()
        {
            InitializeComponent();
        }

        private void blt_query_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            if (cb_dep.IsChecked == true)
            {
                //加入部门选择项
                if(tb_dep.Tag is DepmentInfo)
                {
                    sb.Append(string.Format(" (DeviceDepID = {0}) and ",(tb_dep.Tag as DepmentInfo).Id));
                }
            }
            if(cb_type.IsChecked == true)
            {
                //加入类型选择项
                if(tb_type.Tag is DeviceTypeInfo)
                {
                    sb.Append(string.Format(" (DeviceType = {0}) and ", (tb_dep.Tag as DeviceTypeInfo).Id));
                }
            }
            switch (cb_warType.SelectedIndex)
            {
                case 0:
                    {
                        //正常的设备
                        sb.Append(@" ((DeviceVoltage between DeviceMinVoltage and DeviceMaxVoltage
and DeviceElectricCurrent between DeviceMinElectricCurrent and DeviceMaxElectricCurrent
and DeviceTemperature between DeviceMinTemperature and DeviceMaxTemperature
and DeviceHumidity between DeviceMinHumidity and DeviceMaxHumidity)
and datediff(MINUTE, DeviceLastUpDate, GETDATE()) <= DeviceUpDateTime
and DeviceIsMaintain = 0)");
                    }break;
                case 1:
                    {
                        //电压异常的设备
                        sb.Append(@" (not (DeviceVoltage between DeviceMinVoltage and DeviceMaxVoltage)
and datediff(MINUTE, DeviceLastUpDate, GETDATE()) <= DeviceUpDateTime
and DeviceIsMaintain = 0)");
                    }
                    break;
                case 2:
                    {
                        //电流异常的设备
                        sb.Append(@" (not (DeviceElectricCurrent between DeviceMinElectricCurrent and DeviceMaxElectricCurrent)
and datediff(MINUTE, DeviceLastUpDate, GETDATE()) <= DeviceUpDateTime
and DeviceIsMaintain = 0)");
                    }
                    break;
                case 3:
                    {
                        //温度异常的设备
                        sb.Append(@" (not (DeviceTemperature between DeviceMinTemperature and DeviceMaxTemperature)
and datediff(MINUTE, DeviceLastUpDate, GETDATE()) <= DeviceUpDateTime
and DeviceIsMaintain = 0)");
                    }
                    break;
                case 4:
                    {
                        //湿度异常的设备
                        sb.Append(@" ( not (DeviceHumidity between DeviceMinHumidity and DeviceMaxHumidity)
and datediff(MINUTE, DeviceLastUpDate, GETDATE()) <= DeviceUpDateTime
and DeviceIsMaintain = 0)");
                    }
                    break;
                default:
                    {
                        return;
                    }
            }

            string where = sb.ToString();
            if (!string.IsNullOrEmpty(where))
            {
                List<DeviceNode> nodes = ManagerDBCtrl.GetMng().GetAllDevices(where);
                lv_waringdevices.ItemsSource = nodes;
            }
        }

        private void blt_selDep_Click(object sender, RoutedEventArgs e)
        {
            WindowDepSelected wind = new WindowDepSelected();
            if (wind.ShowDialog() == true)
            {
                this.tb_dep.Text = wind.NowSelectedInfo.Name;
                this.tb_dep.ToolTip = wind.NowSelectedInfo.Des;
                this.tb_dep.Tag = wind.NowSelectedInfo;
            }
        }

        private void blt_seltype_Click(object sender, RoutedEventArgs e)
        {
            WindowDeviceTypeSelected wind = new WindowDeviceTypeSelected();
            if (wind.ShowDialog() == true)
            {
                this.tb_type.Text = wind.NowSelectedInfo.Name;
                this.tb_type.ToolTip = wind.NowSelectedInfo.Des;
                this.tb_type.Tag = wind.NowSelectedInfo;
            }
        }
    }
}
